package com.dhnsoft.shortLink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhnsoft.shortLink.project.common.convention.result.Result;
import com.dhnsoft.shortLink.project.common.convention.result.Results;
import com.dhnsoft.shortLink.project.dto.req.ShortLinkCreateReqDTO;
import com.dhnsoft.shortLink.project.dto.req.ShortLinkPageReqDTO;
import com.dhnsoft.shortLink.project.dto.req.ShortLinkUpdateReqDTO;
import com.dhnsoft.shortLink.project.dto.resp.ShortLinkCountQueryRespDTO;
import com.dhnsoft.shortLink.project.dto.resp.ShortLinkCreateRespDTO;
import com.dhnsoft.shortLink.project.dto.resp.ShortLinkPageRespDTO;
import com.dhnsoft.shortLink.project.service.ShortLinkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: ShortLinkController
 * Package: com.dhnsoft.shortLink.project.controller
 * Description: 短链接控制层
 *
 * @Author dhn
 * @Create 2024/3/21 17:39
 * @Version 1.0
 */

@RestController
@RequiredArgsConstructor
public class ShortLinkController {


    private final ShortLinkService shortLinkService;



    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, HttpServletRequest request, HttpServletResponse response){
        shortLinkService.restoreUrl(shortUri,request,response);
    }

    /**
     * 创建短链接
     * */
    @PostMapping("/api/short-link/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam){
        return Results.success(shortLinkService.createShortLink(requestParam));
    }

    /**
     * 短链接分页
     * */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        return Results.success(shortLinkService.pageShortLink(requestParam));
    }


    /**
     * 查看分组下短链接数量
     * */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkCountQueryRespDTO>> listShortLinkCount(@RequestParam List<String> requestParam){
        return Results.success(shortLinkService.listShortLinkCount(requestParam));
    }

    /**
     * 修改短链接
     * */
    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }

}
